package com.cyyang.shop.controller;

import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cyyang.shop.bean.Admin;
import com.cyyang.shop.service.UserService;

@Controller
public class indexController {
	
	@Autowired
	RedisTemplate<String, Admin> redisTemplate;
	
	@Reference
	UserService userService;
	
	@RequestMapping(value = {"","/","index"})
	public String list(HttpServletRequest req) {
		System.out.println(req.getRemoteAddr());
		return "index";
	}
	@RequestMapping("toLogin")
	public String toLogin(HttpServletRequest req) {
		return "login";
	}
	
	@RequestMapping("login")
	public String login(Admin user) {
		Admin admin = userService.selectUser(user);
		if (admin!=null) {
			redisTemplate.opsForValue().set("user", admin, 1,TimeUnit.MINUTES);
			return "redirect:index";
		}
		return "login";
	}
	
	

}
